package torque.generated;


import java.math.BigDecimal;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.ObjectUtils;
import org.apache.torque.TorqueException;
import org.apache.torque.map.TableMap;
import org.apache.torque.om.BaseObject;
import org.apache.torque.om.ComboKey;
import org.apache.torque.om.DateKey;
import org.apache.torque.om.NumberKey;
import org.apache.torque.om.ObjectKey;
import org.apache.torque.om.SimpleKey;
import org.apache.torque.om.StringKey;
import org.apache.torque.om.Persistent;
import org.apache.torque.util.Criteria;
import org.apache.torque.util.Transaction;





/**
 * les equipements d un troll (ce qu il a sur lui)
 *
 * This class was autogenerated by Torque on:
 *
 * [Wed Oct 16 17:21:00 CEST 2013]
 *
 * You should not use this class directly.  It should not even be
 * extended all references should be to Equiper
 */
public abstract class BaseEquiper extends BaseObject
{
    /** Serial version */
    private static final long serialVersionUID = 1381936860962L;

    /** The Peer class */
    private static final EquiperPeer peer =
        new EquiperPeer();


    /** The value for the nomTroll field */
    private String nomTroll;

    /** The value for the idObjet field */
    private int idObjet;

    /** The value for the idPartie field */
    private String idPartie;


    /**
     * Get the NomTroll
     *
     * @return String
     */
    public String getNomTroll()
    {
        return nomTroll;
    }


    /**
     * Set the value of NomTroll
     *
     * @param v new value
     */
    public void setNomTroll(String v) throws TorqueException
    {

        if (!ObjectUtils.equals(this.nomTroll, v))
        {
            this.nomTroll = v;
            setModified(true);
        }


        if (aTroll != null && !ObjectUtils.equals(aTroll.getNom(), v))
        {
            aTroll = null;
        }

    }

    /**
     * Get the IdObjet
     *
     * @return int
     */
    public int getIdObjet()
    {
        return idObjet;
    }


    /**
     * Set the value of IdObjet
     *
     * @param v new value
     */
    public void setIdObjet(int v) throws TorqueException
    {

        if (this.idObjet != v)
        {
            this.idObjet = v;
            setModified(true);
        }


        if (aObjets != null && !(aObjets.getIdentifiant() == v))
        {
            aObjets = null;
        }

    }

    /**
     * Get the IdPartie
     *
     * @return String
     */
    public String getIdPartie()
    {
        return idPartie;
    }


    /**
     * Set the value of IdPartie
     *
     * @param v new value
     */
    public void setIdPartie(String v) throws TorqueException
    {

        if (!ObjectUtils.equals(this.idPartie, v))
        {
            this.idPartie = v;
            setModified(true);
        }


        if (aPartie != null && !ObjectUtils.equals(aPartie.getIdPartie(), v))
        {
            aPartie = null;
        }

    }

    



    private Partie aPartie;

    /**
     * Declares an association between this object and a Partie object
     *
     * @param v Partie
     * @throws TorqueException
     */
    public void setPartie(Partie v) throws TorqueException
    {
        if (v == null)
        {
            setIdPartie((String) null);
        }
        else
        {
            setIdPartie(v.getIdPartie());
        }
        aPartie = v;
    }


    /**
     * Returns the associated Partie object.
     * If it was not retrieved before, the object is retrieved from
     * the database
     *
     * @return the associated Partie object
     * @throws TorqueException
     */
    public Partie getPartie()
        throws TorqueException
    {
        if (aPartie == null && (!ObjectUtils.equals(this.idPartie, null)))
        {
            aPartie = PartiePeer.retrieveByPK(SimpleKey.keyFor(this.idPartie));
        }
        return aPartie;
    }

    /**
     * Return the associated Partie object
     * If it was not retrieved before, the object is retrieved from
     * the database using the passed connection
     *
     * @param connection the connection used to retrieve the associated object
     *        from the database, if it was not retrieved before
     * @return the associated Partie object
     * @throws TorqueException
     */
    public Partie getPartie(Connection connection)
        throws TorqueException
    {
        if (aPartie == null && (!ObjectUtils.equals(this.idPartie, null)))
        {
            aPartie = PartiePeer.retrieveByPK(SimpleKey.keyFor(this.idPartie), connection);
        }
        return aPartie;
    }

    /**
     * Provides convenient way to set a relationship based on a
     * ObjectKey, for example
     * <code>bar.setFooKey(foo.getPrimaryKey())</code>
     *
     */
    public void setPartieKey(ObjectKey key) throws TorqueException
    {

        setIdPartie(key.toString());
    }




    private Objets aObjets;

    /**
     * Declares an association between this object and a Objets object
     *
     * @param v Objets
     * @throws TorqueException
     */
    public void setObjets(Objets v) throws TorqueException
    {
        if (v == null)
        {
            setIdObjet( 0);
        }
        else
        {
            setIdObjet(v.getIdentifiant());
        }
        aObjets = v;
    }


    /**
     * Returns the associated Objets object.
     * If it was not retrieved before, the object is retrieved from
     * the database
     *
     * @return the associated Objets object
     * @throws TorqueException
     */
    public Objets getObjets()
        throws TorqueException
    {
        if (aObjets == null && (this.idObjet != 0))
        {
            aObjets = ObjetsPeer.retrieveByPK(SimpleKey.keyFor(this.idObjet));
        }
        return aObjets;
    }

    /**
     * Return the associated Objets object
     * If it was not retrieved before, the object is retrieved from
     * the database using the passed connection
     *
     * @param connection the connection used to retrieve the associated object
     *        from the database, if it was not retrieved before
     * @return the associated Objets object
     * @throws TorqueException
     */
    public Objets getObjets(Connection connection)
        throws TorqueException
    {
        if (aObjets == null && (this.idObjet != 0))
        {
            aObjets = ObjetsPeer.retrieveByPK(SimpleKey.keyFor(this.idObjet), connection);
        }
        return aObjets;
    }

    /**
     * Provides convenient way to set a relationship based on a
     * ObjectKey, for example
     * <code>bar.setFooKey(foo.getPrimaryKey())</code>
     *
     */
    public void setObjetsKey(ObjectKey key) throws TorqueException
    {

        setIdObjet(((NumberKey) key).intValue());
    }




    private Troll aTroll;

    /**
     * Declares an association between this object and a Troll object
     *
     * @param v Troll
     * @throws TorqueException
     */
    public void setTroll(Troll v) throws TorqueException
    {
        if (v == null)
        {
            setNomTroll((String) null);
        }
        else
        {
            setNomTroll(v.getNom());
        }
        aTroll = v;
    }


    /**
     * Returns the associated Troll object.
     * If it was not retrieved before, the object is retrieved from
     * the database
     *
     * @return the associated Troll object
     * @throws TorqueException
     */
    public Troll getTroll()
        throws TorqueException
    {
        if (aTroll == null && (!ObjectUtils.equals(this.nomTroll, null)))
        {
            aTroll = TrollPeer.retrieveByPK(SimpleKey.keyFor(this.nomTroll));
        }
        return aTroll;
    }

    /**
     * Return the associated Troll object
     * If it was not retrieved before, the object is retrieved from
     * the database using the passed connection
     *
     * @param connection the connection used to retrieve the associated object
     *        from the database, if it was not retrieved before
     * @return the associated Troll object
     * @throws TorqueException
     */
    public Troll getTroll(Connection connection)
        throws TorqueException
    {
        if (aTroll == null && (!ObjectUtils.equals(this.nomTroll, null)))
        {
            aTroll = TrollPeer.retrieveByPK(SimpleKey.keyFor(this.nomTroll), connection);
        }
        return aTroll;
    }

    /**
     * Provides convenient way to set a relationship based on a
     * ObjectKey, for example
     * <code>bar.setFooKey(foo.getPrimaryKey())</code>
     *
     */
    public void setTrollKey(ObjectKey key) throws TorqueException
    {

        setNomTroll(key.toString());
    }
   
        
    private static List fieldNames = null;

    /**
     * Generate a list of field names.
     *
     * @return a list of field names
     */
    public static synchronized List getFieldNames()
    {
        if (fieldNames == null)
        {
            fieldNames = new ArrayList();
            fieldNames.add("NomTroll");
            fieldNames.add("IdObjet");
            fieldNames.add("IdPartie");
            fieldNames = Collections.unmodifiableList(fieldNames);
        }
        return fieldNames;
    }

    /**
     * Retrieves a field from the object by field (Java) name passed in as a String.
     *
     * @param name field name
     * @return value
     */
    public Object getByName(String name)
    {
        if (name.equals("NomTroll"))
        {
            return getNomTroll();
        }
        if (name.equals("IdObjet"))
        {
            return new Integer(getIdObjet());
        }
        if (name.equals("IdPartie"))
        {
            return getIdPartie();
        }
        return null;
    }

    /**
     * Set a field in the object by field (Java) name.
     *
     * @param name field name
     * @param value field value
     * @return True if value was set, false if not (invalid name / protected field).
     * @throws IllegalArgumentException if object type of value does not match field object type.
     * @throws TorqueException If a problem occurs with the set[Field] method.
     */
    public boolean setByName(String name, Object value )
        throws TorqueException, IllegalArgumentException
    {
        if (name.equals("NomTroll"))
        {
            // Object fields can be null
            if (value != null && ! String.class.isInstance(value))
            {
                throw new IllegalArgumentException("Invalid type of object specified for value in setByName");
            }
            setNomTroll((String) value);
            return true;
        }
        if (name.equals("IdObjet"))
        {
            if (value == null || ! (Integer.class.isInstance(value)))
            {
                throw new IllegalArgumentException("setByName: value parameter was null or not an Integer object.");
            }
            setIdObjet(((Integer) value).intValue());
            return true;
        }
        if (name.equals("IdPartie"))
        {
            // Object fields can be null
            if (value != null && ! String.class.isInstance(value))
            {
                throw new IllegalArgumentException("Invalid type of object specified for value in setByName");
            }
            setIdPartie((String) value);
            return true;
        }
        return false;
    }

    /**
     * Retrieves a field from the object by name passed in
     * as a String.  The String must be one of the static
     * Strings defined in this Class' Peer.
     *
     * @param name peer name
     * @return value
     */
    public Object getByPeerName(String name)
    {
        if (name.equals(EquiperPeer.NOM_TROLL))
        {
            return getNomTroll();
        }
        if (name.equals(EquiperPeer.ID_OBJET))
        {
            return new Integer(getIdObjet());
        }
        if (name.equals(EquiperPeer.ID_PARTIE))
        {
            return getIdPartie();
        }
        return null;
    }

    /**
     * Set field values by Peer Field Name
     *
     * @param name field name
     * @param value field value
     * @return True if value was set, false if not (invalid name / protected field).
     * @throws IllegalArgumentException if object type of value does not match field object type.
     * @throws TorqueException If a problem occurs with the set[Field] method.
     */
    public boolean setByPeerName(String name, Object value)
        throws TorqueException, IllegalArgumentException
    {
      if (EquiperPeer.NOM_TROLL.equals(name))
        {
            return setByName("NomTroll", value);
        }
      if (EquiperPeer.ID_OBJET.equals(name))
        {
            return setByName("IdObjet", value);
        }
      if (EquiperPeer.ID_PARTIE.equals(name))
        {
            return setByName("IdPartie", value);
        }
        return false;
    }

    /**
     * Retrieves a field from the object by Position as specified
     * in the xml schema.  Zero-based.
     *
     * @param pos position in xml schema
     * @return value
     */
    public Object getByPosition(int pos)
    {
        if (pos == 0)
        {
            return getNomTroll();
        }
        if (pos == 1)
        {
            return new Integer(getIdObjet());
        }
        if (pos == 2)
        {
            return getIdPartie();
        }
        return null;
    }

    /**
     * Set field values by its position (zero based) in the XML schema.
     *
     * @param position The field position
     * @param value field value
     * @return True if value was set, false if not (invalid position / protected field).
     * @throws IllegalArgumentException if object type of value does not match field object type.
     * @throws TorqueException If a problem occurs with the set[Field] method.
     */
    public boolean setByPosition(int position, Object value)
        throws TorqueException, IllegalArgumentException
    {
    if (position == 0)
        {
            return setByName("NomTroll", value);
        }
    if (position == 1)
        {
            return setByName("IdObjet", value);
        }
    if (position == 2)
        {
            return setByName("IdPartie", value);
        }
        return false;
    }
     
    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.
     *
     * @throws Exception
     */
    public void save() throws Exception
    {
        save(EquiperPeer.DATABASE_NAME);
    }

    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.
     * Note: this code is here because the method body is
     * auto-generated conditionally and therefore needs to be
     * in this file instead of in the super class, BaseObject.
     *
     * @param dbName
     * @throws TorqueException
     */
    public void save(String dbName) throws TorqueException
    {
        Connection con = null;
        try
        {
            con = Transaction.begin(dbName);
            save(con);
            Transaction.commit(con);
        }
        catch(TorqueException e)
        {
            Transaction.safeRollback(con);
            throw e;
        }
    }

    /** flag to prevent endless save loop, if this object is referenced
        by another object which falls in this transaction. */
    private boolean alreadyInSave = false;
    /**
     * Stores the object in the database.  If the object is new,
     * it inserts it; otherwise an update is performed.  This method
     * is meant to be used as part of a transaction, otherwise use
     * the save() method and the connection details will be handled
     * internally
     *
     * @param con
     * @throws TorqueException
     */
    public void save(Connection con) throws TorqueException
    {
        if (!alreadyInSave)
        {
            alreadyInSave = true;



            // If this object has been modified, then save it to the database.
            if (isModified())
            {
                if (isNew())
                {
                    EquiperPeer.doInsert((Equiper) this, con);
                    setNew(false);
                }
                else
                {
                    EquiperPeer.doUpdate((Equiper) this, con);
                }
            }

            alreadyInSave = false;
        }
    }



    private final SimpleKey[] pks = new SimpleKey[3];
    private final ComboKey comboPK = new ComboKey(pks);

    /**
     * Set the PrimaryKey with an ObjectKey
     *
     * @param key
     */
    public void setPrimaryKey(ObjectKey key) throws TorqueException
    {
        SimpleKey[] keys = (SimpleKey[]) key.getValue();
        setNomTroll(keys[0].toString());
        setIdObjet(((NumberKey)keys[1]).intValue());
        setIdPartie(keys[2].toString());
    }

    /**
     * Set the PrimaryKey using SimpleKeys.
     *
     * @param nomTroll String
     * @param idObjet int
     * @param idPartie String
     */
    public void setPrimaryKey( String nomTroll, int idObjet, String idPartie)
        throws TorqueException
    {
        setNomTroll(nomTroll);
        setIdObjet(idObjet);
        setIdPartie(idPartie);
    }

    /**
     * Set the PrimaryKey using a String.
     */
    public void setPrimaryKey(String key) throws TorqueException
    {
        setPrimaryKey(new ComboKey(key));
    }

    /**
     * returns an id that differentiates this object from others
     * of its class.
     */
    public ObjectKey getPrimaryKey()
    {
        pks[0] = SimpleKey.keyFor(getNomTroll());
        pks[1] = SimpleKey.keyFor(getIdObjet());
        pks[2] = SimpleKey.keyFor(getIdPartie());
        return comboPK;
    }
 

    /**
     * Makes a copy of this object.
     * It creates a new object filling in the simple attributes.
     * It then fills all the association collections and sets the
     * related objects to isNew=true.
     */
    public Equiper copy() throws TorqueException
    {
        return copy(true);
    }

    /**
     * Makes a copy of this object using connection.
     * It creates a new object filling in the simple attributes.
     * It then fills all the association collections and sets the
     * related objects to isNew=true.
     *
     * @param con the database connection to read associated objects.
     */
    public Equiper copy(Connection con) throws TorqueException
    {
        return copy(true, con);
    }

    /**
     * Makes a copy of this object.
     * It creates a new object filling in the simple attributes.
     * If the parameter deepcopy is true, it then fills all the
     * association collections and sets the related objects to
     * isNew=true.
     *
     * @param deepcopy whether to copy the associated objects.
     */
    public Equiper copy(boolean deepcopy) throws TorqueException
    {
        return copyInto(new Equiper(), deepcopy);
    }

    /**
     * Makes a copy of this object using connection.
     * It creates a new object filling in the simple attributes.
     * If the parameter deepcopy is true, it then fills all the
     * association collections and sets the related objects to
     * isNew=true.
     *
     * @param deepcopy whether to copy the associated objects.
     * @param con the database connection to read associated objects.
     */
    public Equiper copy(boolean deepcopy, Connection con) throws TorqueException
    {
        return copyInto(new Equiper(), deepcopy, con);
    }
  
    /**
     * Fills the copyObj with the contents of this object.
     * The associated objects are also copied and treated as new objects.
     *
     * @param copyObj the object to fill.
     */
    protected Equiper copyInto(Equiper copyObj) throws TorqueException
    {
        return copyInto(copyObj, true);
    }

  
    /**
     * Fills the copyObj with the contents of this object using connection.
     * The associated objects are also copied and treated as new objects.
     *
     * @param copyObj the object to fill.
     * @param con the database connection to read associated objects.
     */
    protected Equiper copyInto(Equiper copyObj, Connection con) throws TorqueException
    {
        return copyInto(copyObj, true, con);
    }
  
    /**
     * Fills the copyObj with the contents of this object.
     * If deepcopy is true, The associated objects are also copied
     * and treated as new objects.
     *
     * @param copyObj the object to fill.
     * @param deepcopy whether the associated objects should be copied.
     */
    protected Equiper copyInto(Equiper copyObj, boolean deepcopy) throws TorqueException
    {
        copyObj.setNomTroll(nomTroll);
        copyObj.setIdObjet(idObjet);
        copyObj.setIdPartie(idPartie);

        copyObj.setNomTroll((String)null);
        copyObj.setIdObjet( 0);
        copyObj.setIdPartie((String)null);

        if (deepcopy)
        {
        }
        return copyObj;
    }
        
    
    /**
     * Fills the copyObj with the contents of this object using connection.
     * If deepcopy is true, The associated objects are also copied
     * and treated as new objects.
     *
     * @param copyObj the object to fill.
     * @param deepcopy whether the associated objects should be copied.
     * @param con the database connection to read associated objects.
     */
    protected Equiper copyInto(Equiper copyObj, boolean deepcopy, Connection con) throws TorqueException
    {
        copyObj.setNomTroll(nomTroll);
        copyObj.setIdObjet(idObjet);
        copyObj.setIdPartie(idPartie);

        copyObj.setNomTroll((String)null);
        copyObj.setIdObjet( 0);
        copyObj.setIdPartie((String)null);

        if (deepcopy)
        {
        }
        return copyObj;
    }
    
    

    /**
     * returns a peer instance associated with this om.  Since Peer classes
     * are not to have any instance attributes, this method returns the
     * same instance for all member of this class. The method could therefore
     * be static, but this would prevent one from overriding the behavior.
     */
    public EquiperPeer getPeer()
    {
        return peer;
    }

    /**
     * Retrieves the TableMap object related to this Table data without
     * compiler warnings of using getPeer().getTableMap().
     *
     * @return The associated TableMap object.
     */
    public TableMap getTableMap() throws TorqueException
    {
        return EquiperPeer.getTableMap();
    }


    public String toString()
    {
        StringBuffer str = new StringBuffer();
        str.append("Equiper:\n");
        str.append("NomTroll = ")
           .append(getNomTroll())
           .append("\n");
        str.append("IdObjet = ")
           .append(getIdObjet())
           .append("\n");
        str.append("IdPartie = ")
           .append(getIdPartie())
           .append("\n");
        return(str.toString());
    }
}
